package com.cz.video.website.controller;


import com.cz.video.common.annotation.Log;
import com.cz.video.common.core.controller.BaseController;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.domain.R;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.enums.BusinessType;
import com.cz.video.common.helper.LoginHelper;
import com.cz.video.website.domain.bo.WebsiteHistoryBo;
import com.cz.video.website.domain.vo.WebsiteHistoryVo;
import com.cz.video.website.service.IWebsiteVideoService;
import com.cz.video.website.service.WebsiteHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 历史记录信息 前端控制器
 * </p>
 *
 */
@Tag(name = "历史记录管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/client/history")
public class WebsiteHistoryController extends BaseController {

  private final WebsiteHistoryService historyService;
  private final IWebsiteVideoService videoService;

  /**
   * 分页获取历史记录信息
   *
   * @param historyBo 查询参数
   * @param pageQuery 分页参数
   *
   * @return 分页历史记录信息
   */
  @Operation(summary = "分页获取历史记录信息")
  @GetMapping("/list")
  public TableDataInfo<WebsiteHistoryVo> list(WebsiteHistoryBo historyBo, PageQuery pageQuery) {
      //刷新历史记录的缓存到数据库
      historyService.reflush(LoginHelper.getUserId());
      TableDataInfo<WebsiteHistoryVo> page = historyService.page(historyBo, pageQuery);
      videoService.setCover(Optional.of(page.getRows()).map(collectList->
          collectList.stream().map(WebsiteHistoryVo::getVideoVo)
              .filter(Objects::nonNull).collect(Collectors.toList())
      ).orElse(null));
      return page;
  }

  /**
   * 根据编号获取历史记录详细信息
   *
   * @param id ID
   *
   * @return 历史记录信息
   */

  @Operation(summary = "根据编号获取历史记录详细信息")
  @GetMapping(value = {"/{id}"})
  public R<WebsiteHistoryVo> getInfo(@PathVariable(value = "id") Long id) {
    return R.ok(historyService.get(id));
  }


  /**
   * 删除历史记录
   *
   * @param ids 历史记录
   *
   * @return 影响行数
   */
  @Log(title = "删除历史记录", businessType = BusinessType.DELETE)
  @Operation(summary = "删除历史记录")
  @DeleteMapping("/{ids}")
  public R<Void> remove(@PathVariable("ids")@NotEmpty(message = "没有选择要删除的记录") Long[] ids) {
    return toAjax(historyService.deleteByIds(ids));
  }




}
